package edu.ts.crowdfunding.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.ts.crowdfunding.domain.User;
import edu.ts.crowdfunding.service.UserService;
import edu.ts.crowdfunding.utils.Result;
import edu.ts.crowdfunding.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author kai.jiang
 * @Date 2025/6/24 19:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户接口", description = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "用户登录接口", description = "返回登录结果  1:成功 0:失败")
    public Result login(@RequestBody UserVo userVo) {
        Result result = new Result();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_username", userVo.getUsername());
        queryWrapper.eq("user_password", userVo.getPassword());

        User user = userService.getOne(queryWrapper);
        if (user != null) {
            result.setCode(1);
            result.setData(user);
            return result;
        }
        result.setCode(0);
        return result;
    }

    @GetMapping("/list")
    @Operation(summary = "用户列表接口", description = "返回结果  1:成功 0:失败")
    public Result list() {
        Result result = new Result();


        try {
            List<User> list = userService.list();
            result.setCode(1);
            result.setData(list);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }


        return result;
    }

}
